package com.yuanjin.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.yuanjin.R;
import com.yuanjin.adapter.NoteAdapter;
import com.yuanjin.util.NotesDB;

public class Note_Activity extends Activity implements
		android.view.View.OnClickListener {
	private Button btn_edit;
	private ListView lv;
	private Intent intent;
	private NoteAdapter mNoteAdapter;
	private SQLiteDatabase dbReader;
	private NotesDB notesDB;
	private Cursor cursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_note);
		initView();
		// FileUtils.createSDCardDir();
		//
		// // 获取sd卡根目录地址,并创建图片父目录文件对象和文件的对象;
		// String file_str =
		// Environment.getExternalStorageDirectory().getPath();
		// File mars_file = new File(file_str + "/my_camera");
		// File file_go = new File(file_str + "/my_camera/file.jpg");
		// // 验证sd卡是否正确安装：
		// if (Environment.MEDIA_MOUNTED.equals(Environment
		// .getExternalStorageState())) {
		// // 先创建父目录，如果新创建一个文件的时候，父目录没有存在，那么必须先创建父目录，再新建文件。
		// if (!mars_file.exists()) {
		// mars_file.mkdirs();
		// }
		//
		// /*
		// * //常规情况下，我们这里会 创建子目录，但在这里不用系统拍照完毕后会根据所给的图片路径自动去实现;
		// * if(!file_go.exists()) { try { file_go.createNewFile(); } catch
		// * (IOException e) { }}
		// */} else {
		// Toast.makeText(this, "请先安装好sd卡", Toast.LENGTH_LONG).show();
		// }

	}

	private void initView() {
		lv = (ListView) findViewById(R.id.list);
		btn_edit = (Button) findViewById(R.id.btn_edit);

		btn_edit.setOnClickListener(this);
		notesDB = new NotesDB(this);
		dbReader = notesDB.getReadableDatabase();
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				cursor.moveToPosition(position);
				intent = new Intent(Note_Activity.this,
						Note_selectAct_Activity.class);
				intent.putExtra(NotesDB.ID,
						cursor.getInt(cursor.getColumnIndex(NotesDB.ID)));
				intent.putExtra(NotesDB.CONTENT, cursor.getString(cursor
						.getColumnIndex(NotesDB.CONTENT)));
				intent.putExtra(NotesDB.TIME,
						cursor.getString(cursor.getColumnIndex(NotesDB.TIME)));
				intent.putExtra(NotesDB.PATH,
						cursor.getString(cursor.getColumnIndex(NotesDB.PATH)));
				intent.putExtra(NotesDB.VIDEO,
						cursor.getString(cursor.getColumnIndex(NotesDB.VIDEO)));
				startActivity(intent);

			}
		});

	}

	@Override
	public void onClick(View v) {
		intent = new Intent(this, Note_AddContent_Activity.class);
		switch (v.getId()) {

		case R.id.btn_edit:// 添加新记事
			Log.e("test", "进入点击事件了");
			intent.putExtra("flag", "4");
			startActivity(intent);
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		selectDB();
	}

	/**
	 * 显示数据库的内容
	 */
	private void selectDB() {
		cursor = dbReader.query(NotesDB.TABLE_NAME, null, null, null, null,
				null, null);
		mNoteAdapter = new NoteAdapter(this, cursor);
		lv.setAdapter(mNoteAdapter);
	};

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}
}
